package com.maotouying.owlsmart.youngLadyPage;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.maotouying.owlsmart.R;
import com.maotouying.owlsmart.SmartUtil.SystemUtil;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class youngladyDetailActivity extends AppCompatActivity {
    private ImageView iv_back;

    private ViewPager viewPager;

    private View youngladyDetail, youngladyDiscuss;

    private List<View> viewlist;

    private Button btn_detail, btn_discuss;
    private TextView mty_address, mty_date_add, mty_feature, mty_business, mty_price, mty_discuss, mty_phone, mty_qq, mty_wechat, mty_standby, mty_source, mty_environment;
    private LinearLayout ll_ylsource, ll_ylphone, ll_ylstandby, ll_ylqq, ll_ylwechat,ll_ylenvironment;
    private LinearLayout ll_list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_younglady_detail);

        //顶部框颜色
        SystemUtil.setStatusBarColor(this, this.getResources().getColor(R.color.top_background_color));

        //返回按钮
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        //滑动导航
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        btn_discuss = (Button) findViewById(R.id.btn_discuss);
        btn_detail = (Button) findViewById(R.id.btn_detail);


        //初始化滑动导航
        initView();
        initDetail();
        //拿到返回的参数
        Intent intent = getIntent();
        String Activity = intent.getStringExtra("Activity");
        String mty_id = intent.getStringExtra("mty_id");
        okhttp_text(mty_id);


    }

    //滑动用的布局
    private void initView() {
        //获取布局文件
        LayoutInflater inflater = getLayoutInflater();
        youngladyDetail = inflater.inflate(R.layout.younglady_detail, null);
        youngladyDiscuss = inflater.inflate(R.layout.younglady_discuss, null);
        //将三个View添加进viewlist数组
        viewlist = new ArrayList<View>();
        viewlist.add(youngladyDetail);
        viewlist.add(youngladyDiscuss);
        //设置适配器
        PagerAdapter pagerAdapter = new PagerAdapter() {
            //适配器必须有一下四个方法
            //返回界面数量
            public int getCount() {
                return viewlist.size();
            }

            //判断view和o的对应关系，instantiateItem返回view
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view == o;
            }

            //移除当前界面
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewlist.get(position));
            }

            //添加界面至当前，并返回当前界面
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                container.addView(viewlist.get(position));
                return viewlist.get(position);
            }
        };
        //绑定适配器
        viewPager.setAdapter(pagerAdapter);
        //设置viewPager的初始界面为第一个界面
        viewPager.setCurrentItem(0);
        //添加切换界面的监听器
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                if (position == 0) {
                    btn_detail.setTextColor(Color.parseColor("#00B2EE"));
                    btn_discuss.setTextColor(Color.parseColor("#696969"));
                } else if (position == 1) {
                    btn_discuss.setTextColor(Color.parseColor("#00B2EE"));
                    btn_detail.setTextColor(Color.parseColor("#696969"));
                }

            }

            @Override
            public void onPageSelected(int position) {


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }


    //获取数据的
    public void okhttp_text(String search) {


        //创建okHttpclient的对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建Request,Builder
        Request.Builder builder = new Request.Builder();
        //使用Request.Builder对象,调用Url方法,传入网路路径
        Request.Builder url = builder.url("http://103.45.145.67/testfolder/youngladydetail.php?mty_id=" + search);
        //使用Request.Builder对象,调用builder方法构件request对象
        Request request = url.build();
        //创建一个Call对象,参数就request对象
        Call call = okHttpClient.newCall(request);
        //使用call对象,调用enqueue方法,请求加入调度(异步加载)

        call.enqueue(new Callback() {
            //当请求失败的时候,调用此方法
            @Override
            public void onFailure(Call call, IOException e) {
                //andler.sendEmptyMessage(FALL);
            }

            //当请求成功的时候,调用此方法
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String source = response.body().string();
                System.out.println(source);

                //去除括号直接读

                String result1 = source.replace("[", "");
                String result = result1.replace("]", "");
//
                JSONObject data_key = JSONObject.parseObject(result);
                String mty_province = data_key.getString("mty_province");
                String mty_address = data_key.getString("mty_address");
                String mty_date_add = data_key.getString("mty_date_add");
                String mty_feature = data_key.getString("mty_feature");
                String mty_business = data_key.getString("mty_business");
                String mty_price = data_key.getString("mty_price");
                String mty_discuss = data_key.getString("mty_discuss");
                String mty_phone = data_key.getString("mty_phone");
                String mty_qq = data_key.getString("mty_qq");
                String mty_wechat = data_key.getString("mty_wechat");
                String mty_standby = data_key.getString("mty_standby");

                String mty_source = data_key.getString("mty_source");
                String mty_environment = data_key.getString("mty_environment");
                String mty_photos = data_key.getString("mty_photos");

                Message msg = new Message();
                msg.what = 21;

                Bundle bundle = new Bundle();
                bundle.putString("mty_address", mty_province + "-" + mty_address);
                bundle.putString("mty_feature", mty_feature);
                bundle.putString("mty_business", mty_business);
                bundle.putString("mty_price", mty_price);
                bundle.putString("mty_discuss", mty_discuss);
                bundle.putString("mty_phone", mty_phone);

                bundle.putString("mty_qq", mty_qq);
                bundle.putString("mty_wechat", mty_wechat);
                bundle.putString("mty_source", mty_source);
                bundle.putString("mty_standby", mty_standby);
                bundle.putString("mty_date_add", mty_date_add);
                bundle.putString("mty_environment", mty_environment);
                bundle.putString("mty_photos", mty_photos);
                msg.setData(bundle);
                Thandler.sendMessage(msg);//用activity中的handler发送消息


            }
        });
    }

    private Handler Thandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 21:
                    String mty_address1 = msg.getData().getString("mty_address");
                    String mty_feature1 = msg.getData().getString("mty_feature");
                    String mty_business1 = msg.getData().getString("mty_business");
                    String mty_price1 = msg.getData().getString("mty_price");
                    String mty_discuss1 = msg.getData().getString("mty_discuss");
                    String mty_phone1 = msg.getData().getString("mty_phone");

                    String mty_qq1 = msg.getData().getString("mty_qq");
                    String mty_wechat1 = msg.getData().getString("mty_wechat");
                    String mty_source1 = msg.getData().getString("mty_source");
                    String mty_standby1 = msg.getData().getString("mty_standby");
                    String mty_date_add1 = msg.getData().getString("mty_date_add");
                    String mty_environment1 = msg.getData().getString("mty_environment");

                    String mty_photos1 = msg.getData().getString("mty_photos");


                    mty_address.setText(mty_address1);
                    mty_feature.setText(mty_feature1);
                    mty_business.setText(mty_business1);
                    mty_price.setText(mty_price1);
                    mty_discuss.setText(mty_discuss1);
                    mty_date_add.setText(mty_date_add1);


                    mty_source.setText(mty_source1);
                    mty_phone.setText(mty_phone1);
                    mty_qq.setText(mty_qq1);
                    mty_wechat.setText(mty_wechat1);
                    mty_standby.setText(mty_standby1);
                    mty_environment.setText(mty_environment1);

                    if (!mty_source.getText().toString().equals("")) {
                        ll_ylsource.setVisibility(View.VISIBLE);
                    }
                    if (!mty_environment.getText().toString().equals("")) {
                        ll_ylenvironment.setVisibility(View.VISIBLE);
                    }

                    if (!mty_phone.getText().toString().equals("")) {
                        ll_ylphone.setVisibility(View.VISIBLE);
                        mty_phone.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                try {

                                    String phoneNumber = mty_phone.getText().toString();
                                    if("".equals(phoneNumber)){
                                        //Toast.makeText(PhoneCallActivity.this,"电话号码不能为空",Toast.LENGTH_LONG).show();
                                        return;
                                    }
                                    Intent intent = new Intent();
                                    //需要权限：android.permission.CALL_PHONE
                                    intent.setAction("android.intent.action.CALL");
//                  intent.addCategory("android.intent.category.DEFAULT");所以这一行可以省略
                                    intent.setData(Uri.parse("tel:" + phoneNumber));
                                    //方法内部会自动为Intent添加类别：android.intent.category.DEFAULT
                                    startActivity(intent);
                                }catch (Exception e){
                                   // Toast.makeText(PhoneCallActivity.this,"输入有误",Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    }


                    if (!mty_qq.getText().toString().equals("")) {
                        ll_ylqq.setVisibility(View.VISIBLE);
                        mty_qq.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                try {
                                    //第二种方式：可以跳转到添加好友，如果qq号是好友了，直接聊天
                                    String url = "mqqwpa://im/chat?chat_type=wpa&uin="+mty_qq.getText().toString();//uin是发送过去的qq号码
                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                    }


                    if (!mty_wechat.getText().toString().equals("")) {
                        ll_ylwechat.setVisibility(View.VISIBLE);
                        mty_wechat.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(copy(mty_wechat.getText().toString())){

                                    Toast.makeText(youngladyDetailActivity.this, "已经复制到剪贴板", Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(youngladyDetailActivity.this, "遇到了点问题，试试别的方式吧～", Toast.LENGTH_SHORT).show();

                                }
                            }
                        });
                    }

                    if (!mty_standby.getText().toString().equals("")) {
                        ll_ylstandby.setVisibility(View.VISIBLE);

                    }


                    String mp[] =  mty_photos1.split(",");
                    for (String s : mp){
                        ImageView tvd = new ImageView(youngladyDetailActivity.this);
                        Glide.with(youngladyDetailActivity.this)
                                .load("http://103.45.145.67/testsex/ps/"+s)
                                //.placeholder(R.drawable.ic_default_image)
                                .error(R.drawable.ic_clipimage)
                                .into(tvd);
                        ll_list.addView(tvd);
                    }


                    break;

            }
        }
    };

    //充填和响应事件的
    private void initDetail() {

        //详情下拉
        mty_address = (TextView) youngladyDetail.findViewById(R.id.mty_address);
        mty_feature = (TextView) youngladyDetail.findViewById(R.id.mty_feature);

        mty_business = (TextView) youngladyDetail.findViewById(R.id.mty_business);
        mty_price = (TextView) youngladyDetail.findViewById(R.id.mty_price);

        mty_discuss = (TextView) youngladyDetail.findViewById(R.id.mty_discuss);
        mty_phone = (TextView) youngladyDetail.findViewById(R.id.mty_phone);

        mty_qq = (TextView) youngladyDetail.findViewById(R.id.mty_qq);
        mty_wechat = (TextView) youngladyDetail.findViewById(R.id.mty_wechat);
        mty_source = (TextView) youngladyDetail.findViewById(R.id.mty_source);
        mty_standby = (TextView) youngladyDetail.findViewById(R.id.mty_standby);
        mty_date_add = (TextView) youngladyDetail.findViewById(R.id.mty_date_add);
        mty_environment = (TextView) youngladyDetail.findViewById(R.id.mty_environment);

        ll_ylsource = (LinearLayout) youngladyDetail.findViewById(R.id.ll_ylsource);
        ll_ylphone = (LinearLayout) youngladyDetail.findViewById(R.id.ll_ylphone);
        ll_ylstandby = (LinearLayout) youngladyDetail.findViewById(R.id.ll_ylstandby);
        ll_ylqq = (LinearLayout) youngladyDetail.findViewById(R.id.ll_ylqq);
        ll_ylwechat = (LinearLayout) youngladyDetail.findViewById(R.id.ll_ylwechat);
        ll_list = (LinearLayout) youngladyDetail.findViewById(R.id.ll_list);
        ll_ylenvironment = (LinearLayout) youngladyDetail.findViewById(R.id.ll_ylenvironment);
    }

    /**
     * 复制内容到剪切板
     *
     * @param copyStr
     * @return
     */
    private boolean copy(String copyStr) {
        try {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", copyStr);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}